package service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import dao.impl.ConcernDaoImpl;
import dao.impl.InformMessageDaoImpl;
import dao.impl.UserDaoImpl;
import domian.*;
import redis.clients.jedis.Jedis;
import service.UserService;
import utils.Redis;
import utils.ToolUtil;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

public class ConcernServiceImpl implements service.ConcernService {
    ConcernDaoImpl concernDao = new ConcernDaoImpl();
    UserDaoImpl userDao = new UserDaoImpl();
    InformMessageDaoImpl informMessageDao = new InformMessageDaoImpl();
    UserService us = new UserServiceImpl();
    @Override
    public void concernMessage(Integer uid, String coll,Integer fid ,String nickname) throws IOException {
        //关注时
        if (coll.equals("关注")){
            Concern concerns = concernDao.viewUid(fid); //调用文件的id，先查询到被关注用户的id
            Integer focusUid = concerns.getFocus_uid();
            concernDao.addConcern(uid,focusUid);
            //关注，给被关注的用户发消息
            String nicknames ="【关注】"+nickname+"关注了你";
            informMessageDao.addInform(new InformMessage(focusUid,nicknames,ToolUtil.dateString(new Date())));
        }else { //取消关注时
            Concern concerns = concernDao.viewUid(fid); //调用文件的id，先查询到被关注用户的id
            Integer focusUid = concerns.getFocus_uid();
             concernDao.cancelConcern(focusUid);
            //关注，给被关注的用户发消息
            String nicknames ="【取消关注】"+nickname+"取消了对你的关注";
            informMessageDao.addInform(new InformMessage(focusUid,nicknames,ToolUtil.dateString(new Date())));

        }

    }

    @Override
    public void concernUid(Integer uid,Integer userId, String str) {
        if (str.equals("已关注")){  //在查询页面 点击已关注按钮 按钮变成关注 数据库删除当前数据
            concernDao.cancelConcern(userId);
        }else {   //点击关注  数据库添加关注数据
            concernDao.addConcern(uid,userId);
        }

    }

    @Override
    public PageBean queryConcern(Integer uid, Integer pages, String redisKey) throws IOException {
        //用户关注信息
        List<Concern> concerns1 = concernDao.viewConcern(uid, null, null);
        //关注总数
        Integer size1 = concerns1.size();
        List<Concern> concern = concernDao.viewConcern(uid, (pages-1)*10, 10);
        //用户粉丝信息
        List<Concern> concerns2 = concernDao.fansConcern(uid);
        //粉丝总数
        Integer size2 = concerns2.size();
        //获得总页数
        double ceil = Math.ceil((double) size1 / 10);
        Integer ce = (int) ceil;

        //先查询当前用户的所有信息
        User userById = userDao.findUserById(uid);
       /* Jedis jedis = new Jedis();
        String json = jedis.get("user");
        ObjectMapper om = new ObjectMapper();
        User user = om.readValue(json, User.class);
        Integer user.getFollowsum()=size1;*/

        //将用户粉丝数和关注数填入用户表里
        User users = new User(userById.getUid(),userById.getTel(),userById.getPassword(),userById.getRealname(),userById.getSex(),userById.getHeaderimg(),userById.getNickname(),userById.getBirthday(),userById.getEmail(),userById.getTotal(),size1,size2);
        //修改用户表里的关注和粉丝数
        userDao.updateUser(users);

        //将redis数据库里的数据也改变一下
        UserServiceImpl userService = new UserServiceImpl();
        userService.userUpdate(users,redisKey);

        //返回到servlet
        PageBean pageBean = new PageBean(size1,size2,ce,concern);
        return pageBean;
    }

    @Override
    public PageBean attentionData(Integer uid, Integer pages) {
        //关注用户的资源信息
        List<Collect> collects1 = concernDao.concernMessage(uid, null, null);
        //总条数
        int size = collects1.size();
        List<Collect> collects2 = concernDao.concernMessage(uid, (pages - 1) * 5, 5);

        //总页数
        double ceil = Math.ceil((double) size / 5);
        Integer ce = (int) ceil;
        PageBean pageBean = new PageBean(size,ce,collects2);
        return pageBean;
    }

    @Override
    public PageBean followService(Integer uid) {
        List<Concern> concerns = concernDao.uidFollow(uid);
        PageBean pageBean = new PageBean(concerns);
        return pageBean;
    }

    @Override
    public PageBean documentUserMessageService(Integer fid) {
        List<User> users = concernDao.documentUserMessageDao(fid);
        PageBean pageBean = new PageBean(users);
        return pageBean;
    }
}
